package kr.co.pension.utils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;



/**
 * 페이징 클래스
 * Class Name	:	PaginationInfo.java
 * Description	:	
 * Modification Information 
 * 
 * 		수  정  일			수정자		수정내용
 * 	  --------------   --------    ---------------------------------------------
 * 		2010. 8. 3.	김형욱		최초생성
 * 
 * 
 * @author		:	김형욱
 * @since		:	2010. 8. 3.
 * @version		:	1.0
 */
public class PaginationInfo {
	private int currentPageNo=1; // 현재페이지
	private int recordCountPerPage = 20; // 페이지당보여질 레코드갯수
	private int pageSize = 10; // 페이지사이즈
	private int totalRecordCount; // 총레코드갯수
	private int totalPageCount; // 총페이지갯수
	private int firstPageNoOnPageList; // 첫페이지번호
	private int lastPageNoOnPageList;// 마지막페이지번호
	private int firstRecordIndex; // 첫레코드번호
	private int lastRecordIndex; // 마지막레코드번호
	private String prameter="";//전달되는 파라미터
	private Map paramMap;	//파라미터맵
	private List list = new ArrayList();
	private String toHtml;
	private int nowCnt;//현게시물 번호
	
	public PaginationInfo(){
		
	}
//	public PaginationInfo(Map map){
//		PaginationInfo(map);
//	}
	public PaginationInfo(PaginationInfo pinfo) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{
		SMCommonUtil util = new SMCommonUtil();
		Map p = new HashMap();
		util.beanToMap(pinfo, p);
		PaginationInfo(p);
	}
	
	
	
	public int getNowCnt() {
		return getTotalRecordCount()
		- ((getCurrentPageNo() - 1) * getRecordCountPerPage());
	}
	public void setNowCnt(int nowCnt) {
		this.nowCnt = nowCnt;
	}
	

	
	/**
	 * 관리자 페이징
	 * @param url
	 * @return
	 */
	public String getToHtml(String url) {
		
		StringBuffer sb = new StringBuffer("<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"2\"><tr><td align=\"center\">");
		int iTotalPageCnt = getTotalPageCount();
		int iPageSize = getCurrentPageNo();

		int iFirstPageNoOnPageList = getFirstPageNoOnPageList();
		int iLastPageNoOnPageList = getLastPageNoOnPageList();
		int iCurrentPageNo = getCurrentPageNo();
		
		String param = getPrameter();
		
		if("".equals(param)){
			param="?";
		}else{
			param = "?"+param.substring(1,param.length());
			param=param+"&";
		}
		
		url = url+ param;
		
		//처음 아이콘
		if(iCurrentPageNo!=1){
			sb.append("<a href=\"");
			sb.append(url);
			sb.append("currentPageNo=1\" class=\"prevF\" title=\"First Page\">");
			sb.append("처음");
			sb.append("</a> ");
		}else{
			sb.append("<a href=\"#\" class=\"prevF\" title=\"First Page\">처음");
			sb.append("</a> ");
		}
		
		//두번째 아이콘
		if(iCurrentPageNo>1){
			sb.append("<a href=\"");
			sb.append(url);
			sb.append("currentPageNo=");
			sb.append((iCurrentPageNo-1));
			sb.append("\" class=\"prev\">");
			sb.append("이전");
			sb.append("</a> ");
			

		}else{
			sb.append("<a href=\"#\" class=\"prev\">이전");
			sb.append("</a> ");
		}
		
		for(int i =iFirstPageNoOnPageList;i<=iLastPageNoOnPageList;i++){
			if(i==iCurrentPageNo){
				
				sb.append("<a href=\"#");
				sb.append("\" class=\"number current\"");
				sb.append(" title=\"");
				sb.append(i);
				sb.append("\"><font color='red'><b>");
				sb.append(i);
				sb.append("</b></font></a> ");
			}
			
			if(i !=iCurrentPageNo){
				
				sb.append("<a href=\"");
				sb.append(url);
				sb.append("currentPageNo=");
				sb.append(i);
				sb.append("\" class=\"number\"");
				sb.append(" title=\"");
				sb.append(i);
				sb.append("\">[");
				sb.append(i);
				sb.append("]</a> ");
			}
		}
		
		//토탈 페이지 보다 작을때  다음페이지
		if(iCurrentPageNo < iTotalPageCnt){
			
			sb.append("<a href=\"");
			sb.append(url);
			sb.append("currentPageNo=");
			sb.append((iCurrentPageNo+1));
			sb.append("\" class=\"next\" title=\"next\">");
			sb.append("다음");
			sb.append("</a> ");
			
		}else{
			sb.append("<a href=\"#\" class=\"next\" title=\"다음\">다음");
			sb.append("</a> ");

		}
		
		//토탈 페이지 보다 작을때  마지막페이지
		if(iCurrentPageNo != iTotalPageCnt){
			sb.append("<a href=\"");
			sb.append(url);
			sb.append("currentPageNo=");
			sb.append(iTotalPageCnt);
			sb.append("\" class=\"nextL\" title=\"마지막\">");
			sb.append("마지막");
			sb.append("</a> ");
			
		}else{
			sb.append("<a href=\"#\" class=\"next\" title=\"마지막\">");
			sb.append("마지막");
			sb.append("</a> ");
		}

		sb.append("</td></tr></table>");
		return sb.toString();
	}

	/**
	 * 사용자 페이징
	 * @param url
	 * @return
	 */
	public String getToPubHtml(String url) {
		
		String firstImg = "/pub/images/common/btn/btn_first.gif";
		String prevImg = "/pub/images/common/btn/btn_prev.gif";
		
		String nextImg="/pub/images/common/btn/btn_next.gif";
		String lastImg="/pub/images/common/btn/btn_last.gif";
		
		StringBuffer sb = new StringBuffer("<div class=\"paging\">");
		int iTotalPageCnt = getTotalPageCount();
		int iPageSize = getCurrentPageNo();

		int iFirstPageNoOnPageList = getFirstPageNoOnPageList();
		int iLastPageNoOnPageList = getLastPageNoOnPageList();
		int iCurrentPageNo = getCurrentPageNo();
		
		String param = getPrameter();
		
		if("".equals(param)){
			param="?";
		}else{
			param = "?"+param.substring(1,param.length());
			param=param+"&";
		}
		
		url = url+ param;

		//처음 아이콘
		if(iCurrentPageNo!=1){
			sb.append("<a href=\"");
			sb.append(url);
			sb.append("currentPageNo=1\" class=\"prevF\" title=\"First Page\">");
			sb.append("<img src=\"");
			sb.append(ConstValue.ADMIN_CONTEXT);
			sb.append(firstImg);
			
			sb.append("\" alt=\"처음\" />");
			sb.append("</a> ");
		}else{
			sb.append("<a href=\"#\" class=\"prevF\" title=\"First Page\"><img src=\"");
			sb.append(ConstValue.ADMIN_CONTEXT);
			sb.append(firstImg);
			sb.append("\" alt=\"처음\" /></a> ");
		}
		
		//두번째 아이콘
		if(iCurrentPageNo>1){
			sb.append("<a href=\"");
			sb.append(url);
			sb.append("currentPageNo=");
			sb.append((iCurrentPageNo-1));
			sb.append("\" class=\"prev\">");
			
			
			sb.append("<img src=\"");
			sb.append(ConstValue.ADMIN_CONTEXT);
			sb.append(prevImg);
			sb.append("\" alt=\"이전\" />");
			sb.append("</a> ");
			
//			sb.append("<a href=\""+url+"currentPageNo="+(iCurrentPageNo-1)+"\" title=\"Previous Page\">&laquo; Previous</a>");
		}else{
			sb.append("<a href=\"#\" class=\"prev\"><img src=\"");
			sb.append(ConstValue.ADMIN_CONTEXT);
			sb.append(prevImg);
			sb.append("\" alt=\"이전\" /></a> ");
		}
		
		for(int i =iFirstPageNoOnPageList;i<=iLastPageNoOnPageList;i++){
			if(i==iCurrentPageNo){
				sb.append("<strong class=\"on\">");
				sb.append(i);
				sb.append("</strong> ");
			}
			
			if(i !=iCurrentPageNo){
				
				sb.append("<a href=\"");
				sb.append(url);
				sb.append("currentPageNo=");
				sb.append(i);
				sb.append("\" class=\"number\"");
				sb.append(" title=\"");
				sb.append(i);
				sb.append("\">");
				sb.append(i);
				sb.append("</a> ");
			}
		}
		
		//토탈 페이지 보다 작을때  다음페이지
		if(iCurrentPageNo < iTotalPageCnt){
			
			sb.append("<a href=\"");
			sb.append(url);
			sb.append("currentPageNo=");
			sb.append((iCurrentPageNo+1));
			sb.append("\" class=\"next\" title=\"next\">");
			sb.append("<img src=\"");
			sb.append(ConstValue.ADMIN_CONTEXT);
			sb.append(nextImg);
			sb.append("\" alt=\"다음\" />");
			sb.append("</a> ");
			
		}else{
			sb.append("<a href=\"#\" class=\"next\" title=\"다음\"><img src=\"");
			sb.append(ConstValue.ADMIN_CONTEXT);
			sb.append(nextImg);
			sb.append("\" alt=\"다음\" /></a> ");

		}
		
		//토탈 페이지 보다 작을때  마지막페이지
		if(iCurrentPageNo != iTotalPageCnt){
			sb.append("<a href=\"");
			sb.append(url);
			sb.append("currentPageNo=");
			sb.append(iTotalPageCnt);
			sb.append("\" class=\"nextL\" title=\"마지막\">");
			sb.append("<img src=\"");
			sb.append(ConstValue.ADMIN_CONTEXT);
			sb.append(lastImg);
			sb.append("\" alt=\"마지막\" />");
			sb.append("</a> ");
			
		}else{
			sb.append("<a href=\"#\" class=\"next\" title=\"마지막\"><img src=\"");
			sb.append(ConstValue.ADMIN_CONTEXT);
			sb.append(lastImg);
			sb.append("\" alt=\"마지막\" /></a> ");
		}

		sb.append("</div>");
		return sb.toString();
	}
	
	public String getToAjaxPubHtml(String url) {
		
		String firstImg = "/pub/images/common/btn/btn_first.gif";
		String prevImg = "/pub/images/common/btn/btn_prev.gif";
		
		String nextImg="/pub/images/common/btn/btn_next.gif";
		String lastImg="/pub/images/common/btn/btn_last.gif";
		
		StringBuffer sb = new StringBuffer("<div class=\"paging\">");
		int iTotalPageCnt = getTotalPageCount();
		int iPageSize = getCurrentPageNo();

		int iFirstPageNoOnPageList = getFirstPageNoOnPageList();
		int iLastPageNoOnPageList = getLastPageNoOnPageList();
		int iCurrentPageNo = getCurrentPageNo();
		
		String param = getPrameter();
		
		if("".equals(param)){
//			param="?";
		}else{
//			param = "?"+param.substring(1,param.length());
			param = param.substring(1,param.length());
			param=param+"&";
		}
		
//		url = url+ param;

		//처음 아이콘
		if(iCurrentPageNo!=1){
			sb.append("<a href='#' onclick=\"");
			sb.append(url);
			sb.append("('");
			sb.append(param);
			sb.append("currentPageNo=1')\" class=\"prevF\" title=\"First Page\">");
			sb.append("<img src=\"");
			sb.append(ConstValue.ADMIN_CONTEXT);
			sb.append(firstImg);
			
			sb.append("\" alt=\"처음\" />");
			sb.append("</a> ");
		}else{
			sb.append("<a href=\"#\" class=\"prevF\" title=\"First Page\"><img src=\"");
			sb.append(ConstValue.ADMIN_CONTEXT);
			sb.append(firstImg);
			sb.append("\" alt=\"처음\" /></a> ");
		}
		
		//두번째 아이콘
		if(iCurrentPageNo>1){
			sb.append("<a href='#' onclick=\"");
			sb.append(url);
			sb.append("('");
			sb.append(param);
			sb.append("currentPageNo=");
			sb.append((iCurrentPageNo-1));
			sb.append("');\" class=\"prev\">");
			
			
			sb.append("<img src=\"");
			sb.append(ConstValue.ADMIN_CONTEXT);
			sb.append(prevImg);
			sb.append("\" alt=\"이전\" />");
			sb.append("</a> ");
			
//			sb.append("<a href=\""+url+"currentPageNo="+(iCurrentPageNo-1)+"\" title=\"Previous Page\">&laquo; Previous</a>");
		}else{
			sb.append("<a href=\"#\" class=\"prev\"><img src=\"");
			sb.append(ConstValue.ADMIN_CONTEXT);
			sb.append(prevImg);
			sb.append("\" alt=\"이전\" /></a> ");
		}
		
		for(int i =iFirstPageNoOnPageList;i<=iLastPageNoOnPageList;i++){
			if(i==iCurrentPageNo){
				
				sb.append("<a href=\"#");
				sb.append("\" class=\"number current on\"");
				sb.append(" title=\"");
				sb.append(i);
				sb.append("\">");
				sb.append(i);
				sb.append("</a> ");
			}
			
			if(i !=iCurrentPageNo){
				
				sb.append("<a href='#' onclick=\"");
				sb.append(url);
				sb.append("('");
				sb.append(param);
				sb.append("currentPageNo=");
				sb.append(i);
				sb.append("');\" class=\"number\"");
				sb.append(" title=\"");
				sb.append(i);
				sb.append("\">");
				sb.append(i);
				sb.append("</a> ");
			}
		}
		
		//토탈 페이지 보다 작을때  다음페이지
		if(iCurrentPageNo < iTotalPageCnt){
			
			sb.append("<a href='#' onclick=\"");
			sb.append(url);
			sb.append("('");
			sb.append(param);
			sb.append("currentPageNo=");
			sb.append((iCurrentPageNo+1));
			sb.append("');\" class=\"next\" title=\"next\">");
			sb.append("<img src=\"");
			sb.append(ConstValue.ADMIN_CONTEXT);
			sb.append(nextImg);
			sb.append("\" alt=\"다음\" />");
			sb.append("</a> ");
			
		}else{
			sb.append("<a href=\"#\" class=\"next\" title=\"다음\"><img src=\"");
			sb.append(ConstValue.ADMIN_CONTEXT);
			sb.append(nextImg);
			sb.append("\" alt=\"다음\" /></a> ");

		}
		
		//토탈 페이지 보다 작을때  마지막페이지
		if(iCurrentPageNo != iTotalPageCnt){
			sb.append("<a href='#' onclick=\"");
			sb.append(url);
			sb.append("('");
			sb.append(param);
			sb.append("currentPageNo=");
			sb.append(iTotalPageCnt);
			sb.append("');\" class=\"nextL\" title=\"마지막\">");
			sb.append("<img src=\"");
			sb.append(ConstValue.ADMIN_CONTEXT);
			sb.append(lastImg);
			sb.append("\" alt=\"마지막\" />");
			sb.append("</a> ");
			
		}else{
			sb.append("<a href=\"#\" class=\"next\" title=\"마지막\"><img src=\"");
			sb.append(ConstValue.ADMIN_CONTEXT);
			sb.append(lastImg);
			sb.append("\" alt=\"마지막\" /></a> ");
		}

		sb.append("</div>");
		return sb.toString();
	}
//예전 관리자
//public String getToHtml(String url) {
//
//	
//	StringBuffer sb = new StringBuffer("<div class=\"pagination\">");
//	int iTotalPageCnt = getTotalPageCount();
//	int iPageSize = getCurrentPageNo();
//
//	int iFirstPageNoOnPageList = getFirstPageNoOnPageList();
//	int iLastPageNoOnPageList = getLastPageNoOnPageList();
//	int iCurrentPageNo = getCurrentPageNo();
//	
//	String param = getPrameter();
//	
//	if("".equals(param)){
//		param="?";
//	}else{
//		param = "?"+param.substring(1,param.length());
//		param=param+"&";
//	}
//	
//	url = url+ param;
//	
//	//처음 아이콘
//	if(iCurrentPageNo!=1){
//		sb.append("<a href=\""+url+"currentPageNo=1\" title=\"First Page\">&laquo; First</a>");
//	}else{
//		sb.append("<a href=\"#\" title=\"First Page\">&laquo; First</a>");
//	}
//	
//	//두번째 아이콘
//	if(iCurrentPageNo>1){
//		sb.append("<a href=\""+url+"currentPageNo="+(iCurrentPageNo-1)+"\" title=\"Previous Page\">&laquo; Previous</a>");
//	}else{
//		sb.append("<a href=\"#\" title=\"Previous Page\">&laquo; Previous</a>");
//	}
//	
//	for(int i =iFirstPageNoOnPageList;i<=iLastPageNoOnPageList;i++){
//		if(i==iCurrentPageNo){
//			
//			sb.append("<a href=\"#");
//			sb.append("\" class=\"number current\"");
//			sb.append(" title=\"");
//			sb.append(i);
//			sb.append("\">");
//			sb.append(i);
//			sb.append("</a>");
//		}
//		
//		if(i !=iCurrentPageNo){
//			
//			sb.append("<a href=\"");
//			sb.append(url);
//			sb.append("currentPageNo=");
//			sb.append(i);
//			sb.append("\" class=\"number\"");
//			sb.append(" title=\"");
//			sb.append(i);
//			sb.append("\">");
//			sb.append(i);
//			sb.append("</a>");
//		}
//	}
//	
//	//토탈 페이지 보다 작을때  다음페이지
//	if(iCurrentPageNo < iTotalPageCnt){
//		sb.append("<a href=\""+url+"currentPageNo="+(iCurrentPageNo+1)+"\" title=\"Next Page\">Next &raquo;</a>");
//	}else{
//		sb.append("<a href=\"#\" title=\"Next Page\">Next &raquo;</a>");
//	}
//	
//	//토탈 페이지 보다 작을때  마지막페이지
//	if(iCurrentPageNo != iTotalPageCnt){
//		sb.append("<a href=\""+url+"currentPageNo="+iTotalPageCnt+"\" title=\"Last Page\">Last &raquo;</a>");
//	}else{
//		sb.append("<a href=\"#\" title=\"Last Page\">Last &raquo;</a>");
//	}
//
//	sb.append("</div>");
//	return sb.toString();
//}

	public void setToHtml(String toHtml) {
		this.toHtml = toHtml;
	}
	//초기화 셋팅 map을 변수에 할당한다.
	private void PaginationInfo(Map map) {
		int currnetPageNo = 1;
		if(map.get("currentPageNo")!=null && !"0".equals(map.get("currentPageNo"))){
			currnetPageNo = (Integer)map.get("currentPageNo"); 
		}
		setCurrentPageNo(currnetPageNo);
		
		setParamMap(map);
	}
	
	
	public Map getParamMap() {
		return paramMap;
	}
	public void setParamMap(Map paramMap) {
		this.paramMap = paramMap;
	}
	public String getPrameter(Map param) {
		
		param.remove("currentPageNo");
		param.remove("recordCountPerPage");
		param.remove("totalRecordCount");
		param.remove("totalPageCount");
		param.remove("firstPageNoOnPageList");
		param.remove("lastPageNoOnPageList");
		param.remove("firstRecordIndex");
		param.remove("lastRecordIndex");
		param.remove("prameter");
		param.remove("paramMap");
		param.remove("list");
		
		Set paramKeySet = param.keySet();
		Iterator paramIt = paramKeySet.iterator();
		StringBuffer getParamBuf = new StringBuffer();
		String getParam = "";
		int paramSize = 0;
		while(paramIt.hasNext()){
			String key = (String)paramIt.next();
			getParamBuf.append("&"+key+"="+param.get(key));
			paramSize++;
		}
		return getParamBuf.toString();
	}
	
	public String getPrameter() {
		if(paramMap==null){
			return "";
		}
		
		try { paramMap.remove("currentPageNo"); }catch(Exception e) {}
//		paramMap.remove("recordCountPerPage"); }catch(Exception e) {}
		try { paramMap.remove("totalRecordCount"); }catch(Exception e) {}
		try { paramMap.remove("totalPageCount"); }catch(Exception e) {}
		try { paramMap.remove("firstPageNoOnPageList"); }catch(Exception e) {}
		try { paramMap.remove("lastPageNoOnPageList"); }catch(Exception e) {}
		try { paramMap.remove("firstRecordIndex"); }catch(Exception e) {}
		try { paramMap.remove("lastRecordIndex"); }catch(Exception e) {}
		try { paramMap.remove("prameter"); }catch(Exception e) {}
		try { paramMap.remove("paramMap"); }catch(Exception e) {}
		try { paramMap.remove("list"); }catch(Exception e) {}
		
		Set paramKeySet = paramMap.keySet();
		Iterator paramIt = paramKeySet.iterator();
		StringBuffer getParamBuf = new StringBuffer();
		String getParam = "";
		int paramSize = 0;
		while(paramIt.hasNext()){
			String key = (String)paramIt.next();
			getParamBuf.append("&"+key+"="+paramMap.get(key));
			paramSize++;
		}
		return getParamBuf.toString();
	}

	
	public void setTotalPageCount(int totalPageCount) {
		this.totalPageCount = totalPageCount;
	}
	public void setFirstPageNoOnPageList(int firstPageNoOnPageList) {
		this.firstPageNoOnPageList = firstPageNoOnPageList;
	}
	public void setLastPageNoOnPageList(int lastPageNoOnPageList) {
		this.lastPageNoOnPageList = lastPageNoOnPageList;
	}
	public void setFirstRecordIndex(int firstRecordIndex) {
		this.firstRecordIndex = firstRecordIndex;
	}
	public void setLastRecordIndex(int lastRecordIndex) {
		this.lastRecordIndex = lastRecordIndex;
	}
	public void setPrameter(String prameter) {
		this.prameter = prameter;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public int getRecordCountPerPage() {
		return recordCountPerPage;
	}

	public void setRecordCountPerPage(int recordCountPerPage) {
		this.recordCountPerPage = recordCountPerPage;
		setFirstRecordIndex(getFirstRecordIndex());
		setLastRecordIndex(getLastRecordIndex());
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getCurrentPageNo() {
		return currentPageNo;
	}

	public void setCurrentPageNo(int currentPageNo) {
		this.currentPageNo = currentPageNo;
	}

	public void setTotalRecordCount(int totalRecordCount) {
		this.totalRecordCount = totalRecordCount;
		setFirstRecordIndex(getFirstRecordIndex());
		setLastRecordIndex(getLastRecordIndex());
	}

	public int getTotalRecordCount() {
		return totalRecordCount;
	}

	public int getTotalPageCount() {
		totalPageCount = (getTotalRecordCount() - 1) / getRecordCountPerPage()
				+ 1;
		return totalPageCount;
	}

	public int getFirstPageNo() {
		return 1;
	}

	public int getLastPageNo() {
		return getTotalPageCount();
	}

	public int getFirstPageNoOnPageList() {
		firstPageNoOnPageList = ((getCurrentPageNo() - 1) / getPageSize())
				* getPageSize() + 1;
		return firstPageNoOnPageList;
	}

	public int getLastPageNoOnPageList() {
		lastPageNoOnPageList = (getFirstPageNoOnPageList() + getPageSize()) - 1;
		if (lastPageNoOnPageList > getTotalPageCount())
			lastPageNoOnPageList = getTotalPageCount();
		return lastPageNoOnPageList;
	}

	public int getFirstRecordIndex() {
		firstRecordIndex = (getCurrentPageNo() - 1) * getRecordCountPerPage();
		//오라클일때 1이면 0으로 변경
		if(firstRecordIndex==1){
			firstRecordIndex=0;
		}
		return firstRecordIndex;
	}

	public int getLastRecordIndex() {
		lastRecordIndex = getCurrentPageNo() * getRecordCountPerPage();
		return lastRecordIndex;
	}

	
	/**
	 * tving 페이징 추후 적용
	 * @param totalCount
	 * @param currentPage
	 * @param rowCntPerPage
	 * @param funName
	 * @return
	 */
	public static String Pagination(final int totalCount,
			final int currentPage, final int rowCntPerPage, final String funName) {

		String pageStr = "";

		int startPage = 0;
		int endPage = 0;

		if (totalCount > 0) {
			int pageCount = (totalCount / rowCntPerPage)
					+ (totalCount % rowCntPerPage == 0 ? 0 : 1);
			startPage = currentPage - 4;
			endPage = currentPage + 4;

			if (endPage > pageCount) {
				endPage = pageCount;
			}
			if ((endPage - startPage) < 8) {
				startPage = endPage - 8;
			}
			if ((endPage - startPage) < 8) {
				endPage = startPage + 8;
			}
			if ((endPage <= 10) && (pageCount >= 10)) {
				endPage = 10;
			}
			if ((startPage < 0) || (endPage <= 10) || (startPage == 3)) {
				startPage = 1;
			}
			if (endPage >= (pageCount - 2)) {
				endPage = pageCount;
			}

			if (endPage < 10) {
				endPage = pageCount;
			}
			if (endPage > 10) {
				if (startPage > 1) {
					pageStr += "<a href=\"javascript:" + funName
							+ "('1');\">1</a>&nbsp;";
					pageStr += "<a href=\"javascript:" + funName
							+ "('2');\">2</a>&nbsp;<span>&nbsp;</span>&nbsp;";
				}
			}

			for (int i = startPage; i <= endPage; i++) {
				if (currentPage == i) {
					if (i < 10) {
						pageStr += "<a href=\"javascript:" + funName + "(\'"
								+ i + "\');\" class=\"on\">" + i + "</a>&nbsp;";
					} else {
						pageStr += "<a href=\"javascript:" + funName + "(\'"
								+ i + "\');\" class=\"on\">" + i + "</a>&nbsp;";
					}
				} else {
					if (i < 10) {
						pageStr += "<a href=\"javascript:" + funName + "(\'"
								+ i + "\');\">" + i + "</a>&nbsp;";
					} else {
						pageStr += "<a href=\"javascript:" + funName + "(\'"
								+ i + "\');\">" + i + "</a>&nbsp;";
					}
				}
			}

			if (currentPage < pageCount) {
				if (endPage <= (pageCount - 2)) {
					pageStr += "<span>&nbsp;</span> <a href=\"javascript:"
							+ funName + "(\'" + (pageCount - 1) + "\');\");\">"
							+ (pageCount - 1) + "</a>";
					pageStr += "&nbsp;<a href=\"javascript:" + funName + "(\'"
							+ pageCount + "\');\">" + pageCount + "</a>";
				}
			}
		}
		return pageStr;
	}
}
